package com.ddm.controller;

import com.ddm.service.IItemService;
import com.dto.AjaxResult;
import com.entity.Item;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by he on 2025/3/4 13:27
 */
@RequestMapping("/admin/item")
@RestController
public class ItemController {

    @Autowired
    private IItemService itemService;

    // 根据用品ID查询
    @GetMapping("/{itemId}")
    public AjaxResult getItemById(@PathVariable("itemId") Integer itemId) {
        Item item = itemService.selectItemById(itemId);
        return item != null ? AjaxResult.success(item) : AjaxResult.error("用品未找到");
    }
    // 新增用品
    @PostMapping
    public AjaxResult addItem(@RequestBody Item item) {
        int rows = itemService.insertItem(item);
        return rows > 0 ? AjaxResult.success("新增用品成功") : AjaxResult.error("新增用品失败");
    }

    // 修改用品信息
    @PutMapping
    public AjaxResult updateItem(@RequestBody Item item) {
        int rows = itemService.updateItem(item);
        return rows > 0 ? AjaxResult.success("修改用品信息成功") : AjaxResult.error("修改用品信息失败");
    }

    // 根据用品ID删除
    @DeleteMapping("/{itemId}")
    public AjaxResult deleteItemById(@PathVariable Integer itemId) {
        int rows = itemService.deleteItemById(itemId);
        return rows > 0 ? AjaxResult.success("删除用品成功") : AjaxResult.error("删除用品失败");
    }

    // 批量删除用品
    @DeleteMapping("/batch/{itemIds}")
    public AjaxResult deleteItemBatch(@PathVariable Integer[] itemIds) {
        int rows = itemService.deleteItemByIds(itemIds);
        return rows > 0 ? AjaxResult.success("批量删除用品成功") : AjaxResult.error("批量删除用品失败");
    }

    // 分页查询用品
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,@RequestParam(required = false) Item item) {
        PageHelper.startPage(pageNum, pageSize);
        List<Item> list = itemService.selectItemList(item);
        PageInfo<Item> pageInfo = new PageInfo<>(list);
        return AjaxResult.success(pageInfo);
    }
}